/***
 * Copyright (c) 2011 readyState Software Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package mapviewballoons.example.custom;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import sample.classes.MainActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.text.Html;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.apps.realtyportal.HomeActivity;
import com.apps.realtyportal.LoginActivity;
import com.apps.realtyportal.R;
import com.google.android.maps.OverlayItem;
import com.jason.apps.dashboard.CreateAccount;
import com.panorama.xtian.ShowPanorama;
import com.readystatesoftware.mapviewballoons.BalloonOverlayView;
import com.shanu.apps.details.Detalye;
import com.shanu.apps.details.ShowPropertyDetail;

public class CustomBalloonOverlayView<Item extends OverlayItem> extends
		BalloonOverlayView<CustomOverlayItem> {

	private TextView title;
	private TextView snippet;
	private TextView address;
	private TextView pid;
	private TextView pprice;
	private TextView pptype;
	private TextView ppagent;
	private TextView ppcontact;
	private ImageView image;
	private TextView _1stLink;
	private TextView _2ndLink;
	private Detalye detal;
	public static int panoramaId = 0;

	public CustomBalloonOverlayView(Context context, int balloonBottomOffset) {
		super(context, balloonBottomOffset);
	}

	@Override
	public void removeViewAt(int index) {
		// TODO Auto-generated method stub
		super.removeViewAt(index);
	}

	@Override
	protected void setupView(final Context context, final ViewGroup parent) {

		// inflate our custom layout into parent
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.balloon_overlay_example2, parent);
		detal = new Detalye();
		// setup our fields
		title = (TextView) v.findViewById(R.id.balloon_item_title);
		snippet = (TextView) v.findViewById(R.id.balloon_item_snippet);
		address = (TextView) v.findViewById(R.id.balloon_item_address);
		pid = (TextView) v.findViewById(R.id.property_id);
		pprice = (TextView) v.findViewById(R.id.property_price);
		pptype = (TextView) v.findViewById(R.id.property_type);
		ppagent = (TextView) v.findViewById(R.id.property_agent);
		image = (ImageView) v.findViewById(R.id.balloon_item_image);
		ppcontact = (TextView) v.findViewById(R.id.property_contact);
		;

		_1stLink = (TextView) v.findViewById(R.id.balloon_item_snippet_1);
		_1stLink.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Log.i("nakasud", "first");
				String psid = pid.getText().toString();
				String psname = title.getText().toString();
				String psdescription = snippet.getText().toString();
				String psaddress = address.getText().toString();
				String psprice = pprice.getText().toString();
				String pstype = pptype.getText().toString();
				String psagent = ppagent.getText().toString();
				// ((HomeActivity) context).finish();
				//
				// Intent intent = new Intent(context, RegisterActivity.class);
				// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); //
				// important
				// // step
				// context.startActivity(intent);
				panoramaId = Integer.parseInt(pid.getText().toString());
				Toast.makeText(getContext(), "directing to panorama ",
						Toast.LENGTH_LONG).show();
				// Toast.makeText(getContext(),
				// "DATA "+psid+""+psname+""+psdescription+""+psaddress+""+psprice+""+pstype+""+psagent,
				// Toast.LENGTH_LONG).show();
				// Intent intent = new Intent(context,
				// ShowPropertyDetail.class);
				// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
				// context.startActivity(intent);
				//
				// for panorama
				Intent intent = new Intent(context, ShowPanorama.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
				context.startActivity(intent);

			}
			// your code here
		});
		_2ndLink = (TextView) v.findViewById(R.id.balloon_item_snippet_2);
		_2ndLink.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Log.i("nakasud", "2nd");
				// ((HomeActivity) context).finishActivity(0);
				// Intent intent = new Intent(context, MainActivity.class);
				// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); //
				// important
				// // step
				// context.startActivity(intent);
				String psid = pid.getText().toString();
				String psname = title.getText().toString();
				String psdescription = snippet.getText().toString();
				String psaddress = address.getText().toString();
				String psprice = pprice.getText().toString();
				String pstype = pptype.getText().toString();
				String psagent = ppagent.getText().toString();
				String sppcontact = ppcontact.getText().toString();

				int ipsid = Integer.valueOf(psid).intValue();

				// new Detalye().setText(psname, psaddress, psprice, psagent,
				// sppcontact, psdescription, ipsid, pstype);
				panoramaId = Integer.parseInt(pid.getText().toString());
				// new Detalye().setText(psname, psaddress, pprice, aname, con,
				// des, id, ttype)
				// if(HomeActivity.islogin){

				Toast.makeText(getContext(), "directing to view " + psid,
						Toast.LENGTH_LONG).show();
				Intent i = new Intent(context, Detalye.class);
				i.putExtra("propertyid", psid);
				i.putExtra("propertyname", psname);
				i.putExtra("propertydescription", psdescription);
				i.putExtra("propertyaddress", psaddress);
				i.putExtra("propertyprice", psprice);
				i.putExtra("propertytype", pstype);
				i.putExtra("propertyagent", psagent);
				i.putExtra("contact", sppcontact);
				context.startActivity(i);
				// }
				// else{
				// //finish();
				// context.startActivity(new Intent(getContext(),
				// LoginActivity.class));
				//
				// }

			}
			// your code here
		});
	}

	@Override
	protected void setBalloonData(CustomOverlayItem item, ViewGroup parent) {

		// map our custom item data to fields
		title.setText(item.getTitle());
		snippet.setText(item.getSnippet());
		address.setText(item.getPAddress());
		pid.setText(item.getSPid());
		pprice.setText(item.getSPPrice());
		pptype.setText(item.getSPType());
		ppagent.setText(item.getSPAgent());
		ppcontact.setText(item.getSPContact());
		_1stLink.setText(Html
				.fromHtml("<font color='blue'><u><b>Panorama View?</b></u></font>"));

		_1stLink.setMovementMethod(LinkMovementMethod.getInstance());
		_2ndLink.setText(Html
				.fromHtml("<font color='blue'><u><b>View Details?</b></u></font>"));

		// SpannableString ss=new SpannableString("Panorama View");
		// ss.setSpan(new ForegroundColorSpan(Color.RED), 0,
		// "Panorama View".length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
		// _1stLink.setText(ss);
		// get remote image from network.
		// bitmap results would normally be cached, but this is good enough for
		// demo purpose.
		image.setImageResource(R.drawable.icon);
		new FetchImageTask() {
			protected void onPostExecute(Bitmap result) {
				if (result != null) {
					image.setImageBitmap(result);
				}
			}
		}.execute(item.getImageURL());

	}

	private class FetchImageTask extends AsyncTask<String, Integer, Bitmap> {
		@Override
		protected Bitmap doInBackground(String... arg0) {
			Bitmap b = null;
			try {
				b = BitmapFactory.decodeStream((InputStream) new URL(arg0[0])
						.getContent());
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return b;
		}
	}

}
